#pragma once
#include <QtCore/QObject>
#include <QtXml/QDomDocument>

class Serializable :
	public QObject
{
	Q_OBJECT

#pragma region Constructor
public:
	Serializable(void);
	~Serializable(void);

#pragma endregion

#pragma region Public Methods

public:
	virtual bool Serialize(QDomDocument * doc, QDomElement * parent) = 0;
	virtual bool Deserialize(QDomDocument * doc, QDomElement * parent) = 0;

#pragma endregion

#pragma region Helper Methods

protected:
	bool SerializeProps(QDomDocument* doc, QDomElement *tag);
	bool DeserializeProps(QDomDocument* doc, QDomElement *tag);

	template <class t> bool SerializeList(QDomDocument* doc, QDomElement *tag, const QList<t *> & list, const char * name)
	{
		QListIterator<t *> i(list);
		while (i.hasNext())
		{
			QDomElement child = doc->createElement(name);
			tag->appendChild(child);
			i.next()->Serialize(doc, &child);
		}

		return true;
	}

	template <class t> bool DeserializeList(QDomDocument* doc, QDomElement *parent, QList<t *> & list, const char * name)
	{
		QDomElement tag = parent->firstChildElement(name);
		while (!tag.isNull())
		{
			t * item = new t();

			item->Deserialize(doc, &tag);

			list.append(item);

			tag = tag.nextSiblingElement(name);
		}

		return true;
	}

#pragma endregion

};

